var uploadbutton;
var progressBar;
var progressBarText;
var progressBarBoxContent;
var resultMsg;

function refreshProgress()
{
    UploadMonitor.getUploadInfo("",updateProgress);
}

function updateProgress(uploadInfo)
{
    if (uploadInfo.inProgress)
    {
        uploadbutton.disabled = true;
        getElement('uploadfile').disabled = true;
        resultMsg.style.display = 'none';

        var fileIndex = uploadInfo.fileIndex;
        var progressPercent = Math.ceil((uploadInfo.bytesRead / uploadInfo.totalSize) * 100);
        progressBarText.innerHTML = '上传进度: ' + progressPercent + '%';
        progressBarBoxContent.style.width = parseInt(progressPercent * 3.5) + 'px';

        window.setTimeout('refreshProgress()', 1000);
    }
    else
    {   
        uploadbutton.disabled = false;
    	getElement('uploadfile').disabled = false;
    	progressBar.style.display = 'none';
    	resultMsg.style.display = '';        
    }
    return true;
}


function startProgress()
{
    uploadbutton = getElement('uploadbutton');
	progressBar =  getElement('progressBar');
  	progressBarText =  getElement('progressBarText');
  	progressBarBoxContent = getElement('progressBarBoxContent');
 	resultMsg = getElement('resultMsg');    
    
    progressBar.style.display = 'block';
    progressBarText.innerHTML = '上传进度: 0%';  
    
    uploadbutton.disabled = true;

    // wait a little while to make sure the upload has started ..
    window.setTimeout("refreshProgress()", 1500);
    return true;
}

function getElement(name) 
{    
	if (document.getElementById) {    
		return document.getElementById(name);    
	} else if (document.all) {   
		return document.all[name];    
	} else if (document.layers) {    
		return document.layers[name];    
	}    
} 

function trim(s)
{
	return s.replace(/(^\s*)|(\s*$)/g, "");	
}

function sl_check_upload_limit(field,limitTypeNames)
{
	var value = trim(field.value);
	if(value.length<=0)
	{
		alert("请选择上传文件！");
		return false;
	}
	var fileType = value.match(/^(.*)(\.)(.{1,8})$/)[3];
	if(limitTypeNames.toUpperCase().indexOf(fileType.toUpperCase())<0)
	{
		alert("文件上传类型只允许\""+limitTypeNames+"\"！");
		return false;		
	}
	return true;
}
